﻿/***************************************************COPYRIGHT***************************************
* Copyright (c) 2008, Whiteboard-IT
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that 
* the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following 
 * disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the distribution.
*
* Neither the name of Whiteboard-IT, LLC nor the names of its contributors may be used to endorse or promote 
 * products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
*****************************************************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using Mite.Configuration;

namespace Mite
{
	/// <summary>
	/// A table in a relational database.
	/// </summary>
	public class Table
	{
		/// <summary>
		/// Creates an instance of table.
		/// </summary>
		public Table(string tableName, params Column[] columns)
		{
			name = tableName;
			this.columns = columns.ToList();
			this.columns.ForEach(column => column.Table = this);
		}

		/// <summary>
		/// Creates an instance of table.
		/// </summary>
		public Table(string tableName, ForeignKey[] foreignKeys, params Column[] columns) : this(tableName, columns)
		{
			this.foreignKeys = foreignKeys;
		}

		/// <summary>
		/// Name of table.
		/// </summary>
		public string Name
		{
			get { return name; }
		}

		/// <summary>
		/// Columns in table.
		/// </summary>
		public IEnumerable<Column> Columns
		{
			get { return columns; }
		}

		/// <summary>
		/// Foreign keys constraining table.
		/// </summary>
		public IEnumerable<ForeignKey> ForeignKeys
		{
			get { return foreignKeys; }
		}

		/// <summary>
		/// Add a column to the table.
		/// </summary>
		/// <param name="column">Column to add.</param>
		public void AddColumn(Column column)
		{
			Guard.ArgumentNotNull("column", column);

			column.Table = this;
			columns.Add(column);
		}

		/// <summary>
		/// Drop <paramref name="column"/> from the table.
		/// </summary>
		/// <param name="column">The column to drop.</param>
		public void DropColumn(Column column)
		{
			Guard.ArgumentNotNull("column", column);

			columns.Remove(column);
		}

		/// <summary>
		/// Add <paramref name="foreignKey"/> constraint to this table.
		/// </summary>
		/// <param name="foreignKey">The foreign key to be added to the table.</param>
		public void AddForeignKey(ForeignKey foreignKey)
		{
			Guard.ArgumentNotNull("foreignKey", foreignKey);

			foreach (Column column in foreignKey.Columns)
			{
				if (Columns.DoesNotContain(column))
					AddColumn(column);
			}
		}

		/// <summary>
		/// Create a foreign key.
		/// </summary>
		/// <param name="table">Reference table for the foreign key.</param>
		public ForeignKey CreateForeignKeyTo(Table table)
		{
			Guard.ArgumentNotNull("table", table);

			if (ForeignKeys.Contains(tbl => tbl.ReferenceTable.Equals(table)))
			{
				throw new InvalidOperationException(string.Format("{0} already contains a foreign key constraint for {1}.", this,
				                                                  table));
			}

			var key = new ForeignKey(this, table);
			foreignKeys.Add(key);

			return key;
		}

		/// <summary>
		/// Drops the foreign key to <paramref name="table"/>.
		/// </summary>
		/// <param name="table">Reference table of the foreign key to drop.</param>
		public void DropForeignKeyTo(Table table)
		{
			Guard.ArgumentNotNull("table", table);

			ForeignKey foreignKey = ForeignKeys.SingleOrDefault(key => key.Table.Equals(table));
			foreignKeys.Remove(foreignKey);
		}

		/// <summary>
		/// Change name of table to <paramref name="name"/>.
		/// </summary>
		public void Rename(string name)
		{
			this.name = name;
		}

		public override int GetHashCode()
		{
			return Name.GetHashCode();
		}

		public override bool Equals(object obj)
		{
			if (obj == null)
				return false;

			var t = obj as Table;
			if (t == null)
				return false;

			return t.Name.Equals(Name);
		}

		public override string ToString()
		{
			return Name;
		}

		private readonly IList<Column> columns;

		private readonly IList<ForeignKey> foreignKeys;

		private string name;
	}
}